//rawset(_G, "RespawnTable", {})

local stayweapon = CV_RegisterVar({
	name = "sv_weaponsstay",
	defaultvalue = "True",
	PossibleValue = CV_TrueFalse,
	flags = CV_NETVAR,
})

local doubleammo = CV_RegisterVar({
	name = "sv_doubleammo",
	defaultvalue = "Default",
	PossibleValue = {Default = -1, Off = 0, On = 1},
	flags = CV_NETVAR,
})

local function ShouldDoubleAmmo()
	if not (gametyperules & GTR_CAMPAIGN) and doubleammo.value == -1 then return true end //check we're not in a campaign to give out double ammo, match modes give out double ammo but maybe I should also make this check a different rule.
	if doubleammo.value == 1 then return true end
	return false
end

function A_GiveAmmo(actor, var1, var2)
	if var1 == nil then
		var1 = 1 //var 1 = type
	end
	if var2 == nil then
		var2 = 1 //var 2 = amount
	end
	if var1 == 0 then return end //infinite ammo slot xd
	if ShouldDoubleAmmo() then
		var2 = $*2
	end
	actor.target.player.ammolist[var1] = min($ + var2, RFW.Ammoids[var1][2])
	GLFlash(actor.target.player, 73, 2)
end

function A_GiveWeapon(actor, var1, var2) //var 1 = slot
	RFW.GivePlayerWeapon(actor.target.player, var1, var2) //var 2 = weapon constant
	if actor.weapon_halfammo then
		A_GiveAmmo(actor, RFW.Weapons[var1][var2]["AmmoType"], RFW.Weapons[var1][var2]["PickupAmmo"]/2) //give the player ammo with the gun
	elseif not (actor.weapon_noammo) then
		A_GiveAmmo(actor, RFW.Weapons[var1][var2]["AmmoType"], RFW.Weapons[var1][var2]["PickupAmmo"]) //give the player ammo with the gun
	end
	//A_GiveAmmo(actor, RFW.Weapons[var1][var2]["AmmoType"], RFW.Weapons[var1][var2]["PickupAmmo"]) //give the player ammo with the gun
	GLFlash(actor.target.player, 73, 5)
end //you shouldn't need to manually use this if your weapon is set up properly

function A_HealPlayer(actor, var1, var2) //var 1 = Health to give
	if var1 == nil then
		var1 = actor.info.healthgive or 25 //var 1 LOWER = Armor to give
	end
	if var2 == nil then
		var2 = actor.info.healthcap or 0
	end
	Playerheal(actor.target.player, false, var1, var2)//var 2 = Cap, value of 0 is uncapped
	GLFlash(actor.target.player, 73, 5)
end

function A_ArmorPlayer(actor, var1, var2)
	local var1l
	local var1u
	if var1 then
		var1l = var1 & 65535 //var 1 LOWER = Armor to give
		var1u = var1 >> 16//var 1 UPPER = Cap, value of 0 is uncapped
	else
		var1l = actor.info.armorgive or 25
		var1u = actor.info.armorcap or 0
	end
	if var2 == nil then
		var2 = actor.info.protectionvalue or 0
	end
	Playerheal(actor.target.player, true, var1l, var1u, var2) //var 2 = Protection Value, value of 0 keeps previous protection value, or sets it to the default value if we didnt have armor before.
	GLFlash(actor.target.player, 112, 5) 
end

local function CanGainWeapon(p, s) //if we have something to gain from the weapon regardless of if its picked up
	local hasweapon = RFW.PlayerHasWeapon(p, s.info.weaponslot, s.info.weaponconstant)
	local msw = (multiplayer and stayweapon.value)
	local Ammotype = RFW.Weapons[s.info.weaponslot][s.info.weaponconstant]["AmmoType"] or 0
	if hasweapon and ((Ammotype ~= 0 and p.ammolist[Ammotype] >= RFW.Ammoids[Ammotype][2]) or Ammotype == 0) then return false end //we already have the weapon and max ammo, nothing to gain
	if msw and hasweapon return false end //we can never gain anything if weapons should stay and we already have the weapon
	if not hasweapon then return true end //if we don't have the weapon then we always have something to gain from the weapon
	if not msw and ((Ammotype ~= 0 and p.ammolist[Ammotype] < RFW.Ammoids[Ammotype][2]) or Ammotype == 0) then return true end //if we don't have max ammo AND weapons shouldn't stay on pick up than we can gain something
	//maybe there should be more checks but i cant think of any more.
	return false
end

local function CanTakeWeapon(p, s) //if we can physically remove the weapon from the map
	if not CanGainWeapon(p, s) then return false end //we have nothing to gain, only time we ever pick up weapons is if we have something to gain in the first place
	if (multiplayer and stayweapon.value) then return false end //never pick up weapons in mp if weaponsstay is on unless its a dropped weapon
	local Ammotype = RFW.Weapons[s.info.weaponslot][s.info.weaponconstant]["AmmoType"] //cache this for a check we're about to do
	if RFW.PlayerHasWeapon(p, s.info.weaponslot, s.info.weaponconstant) and p.ammolist[Ammotype] >= RFW.Ammoids[Ammotype][2] then return false end //if we have the weapon and are full on ammo then dont pick up
	return true //otherwise we *should* be able to pick up the weapon normally
end //RFW.Ammoids[var1][2]

addHook("TouchSpecial", function(s, mo)
	if not (s and s.valid) then return false end
	if not s.info.weaponpickup then return false end
	if CanTakeWeapon(mo.player, s) then
		s.target = mo //we can take the weapon, so take it!
		local FUCK = RFW.PlayerHasWeapon(mo.player, s.info.weaponslot, s.info.weaponconstant) //do we already have this weapon?
		A_GiveWeapon(s, s.info.weaponslot, s.info.weaponconstant) //take the the weapon now
		//if not FUCK then //if we just got the weapon from this
		//	RFW.TrySelectWeapon(mo.player, s.info.weaponslot, s.info.weaponconstant) //lets try and select it
		//
		/*if s.spawnpoint and CV_FindVar(respawnitem) then
			RespawnTable[#RespawnTable+1] = CV_FindVar(respawnitemtime)*35
		end*/
		if s.droppedweapon then
			P_RemoveMobj(s)
			return true
		else
			return false //we've picked up the weapon, collide with the pick up (should automatically send it to its death state).
		end
	elseif CanGainWeapon(mo.player, s) then 
		s.target = mo //we can gain stuff from the weapon but we shouldn't outright pick up the weapon.
		local FUCK = RFW.PlayerHasWeapon(mo.player, s.info.weaponslot, s.info.weaponconstant) //blah blah
		A_GiveWeapon(s, s.info.weaponslot, s.info.weaponconstant) //grant the weapon
		//if not FUCK then //i can barely take it anymore
		//	RFW.TrySelectWeapon(mo.player, s.info.weaponslot, s.info.weaponconstant)
		//end
		//RFW.TrySelectWeapon(mo.player, s.info.weaponslot, s.info.weaponconstant)
		if s.droppedweapon then
			P_RemoveMobj(s) //remove anyways
			return true
		else
			return true //we want to leave the weapon for other players
		end
	else
		return true //we shouldn't pick up or gain anything from this pickup, ignore it
	end
	//
	return true //don't interact if we somehow skipped the if entirely.
end)

local function CanHeal(p, armor, cap, prot)
	if cap == 0 then return true end //we can always pick up bonuses regardless of our health (even if we have max health.)
	if not armor then //check health
		if p.phealth >= cap and cap ~= 0 then return false end
	else //check armor
		if p.parmor >= cap and cap ~= 0 and p.papval >= prot then return false end
	end
	return true
end

addHook("TouchSpecial", function(s, mo)
	if not (s and s.valid) then return false end
	if not s.info.healthpickup then return false end
	if CanHeal(mo.player, false, s.info.healthcap or 0) then
		s.target = mo
		Playerheal(s.target.player, false, s.info.healthgive, s.info.healthcap, 0)
		if s.info.armorpickup then
			GLFlash(s.target.player, 132, 5)
		else
			GLFlash(s.target.player, 73, 5)
		end
		return false
	end
	if s.info.armorpickup then return not (CanHeal(mo.player, true, s.info.armorcap or 0, s.info.protectionvalue or 0)) end
	return true
end)

addHook("TouchSpecial", function(s, mo)
	if not (s and s.valid) then return false end
	if not s.info.armorpickup then return false end
	if CanHeal(mo.player, true, s.info.armorcap or 0, s.info.protectionvalue or 0) then
		s.target = mo
		Playerheal(s.target.player, true, s.info.armorgive, s.info.armorcap, s.info.protectionvalue or 0)
		if s.info.healthpickup then
			GLFlash(s.target.player, 132, 5)
		else
			GLFlash(s.target.player, 112, 5)
		end
		return false
	end
	if s.info.healthpickup then return not (CanHeal(mo.player, false, s.info.healthcap or 0)) end
	return true
end)

local function CanTakeAmmo(p, id)
	if p.ammolist[id] >= RFW.Ammoids[id][2] then return false end
	return true
end

addHook("TouchSpecial", function(s, mo)
	if not (s and s.valid) then return false end
	if not s.info.ammopickup then return false end
	if CanTakeAmmo(mo.player, s.info.ammotype or 1) then
		s.target = mo
		A_GiveAmmo(s, s.info.ammotype, s.info.ammoamount)
		GLFlash(s.target.player, 73, 2)
		return false
	end
	return true
end)

addHook("MobjDeath", function(mo)
	if not (mo.type & (MF_ENEMY|MF_BOSS)) then return end
	if not mo.info.dropitem then return end
	local item = P_SpawnMobjFromMobj(mo, 0, 0, 0, mo.info.dropitem)
	if item.info.weaponpickup then
		item.droppedweapon = true
		item.weapon_halfammo = true
	end
end)

/*addHook("ThinkFrame", function()
	if not RespawnTable then return end
	for k, v in ipairs(RespawnTable) do
	
	end
end)*/



/*if armor == false or armor == 0 then //we're healing health, not armor
		if player.phealth >= cap and cap ~= 0 then return true //override collision for touchspecial hook, don't pick up item
		elseif player.phealth + amount > cap and cap ~= 0 then //check if uhhh uhmmm uhhhhhhhh i forgot lol
		//check if we'll exceed the health cap by healing off this
			local newhealth = max(0, min((cap - player.phealth), amount)) //new healing value so we can reach the cap, we avoid setting the health directly as safety
			player.phealth = $ + newhealth
		else
			player.phealth = $ + amount
		end
	else //we're healing armor, not health
		if player.parmor >= cap and cap ~= 0 then return true
		elseif player.parmor + amount > cap and cap ~= 0 then
			local newarmor = max(0, min((cap - player.parmor), amount))
			player.parmor = $ + newarmor
		else
			player.parmor = $ + amount
		end
		if protvalue ~= 0 then
			player.papval = protvalue
		end
	end
	return false*/